<?php

/**
 * 店魔盒
 * Class DMH
 */
class DMH
{
    private $client_id = 'client_id';    //  你的client_id
    private $client_secret = 'client_secret';   //  你的client_secret
    public $baseurl = 'long.jshiyong.com';
    public $Authorization='';
    public $apiurl = "";

    public function SendaApi($param, $apiType,$type='')
    {
        $this->login();
        $header = array(
            'Content-Type:' . 'application/json;',
            'Authorization:'.'Bearer ' .$this->Authorization
        );
        if(!$type){
            $param = $this->apiparam($param,$apiType,true);
            if(isset($param['keywords'])){
                if(isset($param['keywords'][0])){
                    foreach ($param['keywords'][0] as $id=>$v){
                        if (strpos($id, 'Style')) {
                            if($v==4){
                                $param['keywords'][0][$id]=3;
                            }
                        }
                    }
                }
            }
            if(isset($param['taskStyle'])){
                if($param['taskStyle']==4){
                    $param['taskStyle']=3;
                }
            }

        }
        $param=json_encode($param);

      //  $param='';

        //echo $param; exit;
        //echo $param;exit;
     /*   echo $param; exit;
      echo '{"browseComment":1,"browseOther":1,"dayNum":1,"endDate":"2020-11-11","fgType":1,"itemId":"140891244028","itemUrl":"https://mobile.yangkeduo.com/goods.html?goods_id=140891244028","keywords":[{"keyword":"衣服","num":"1","taskStyle":3,"taskScatter":"0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0"}],"percent":1.2,"pvuv":1,"shopAround":2,"shopUrl":"https://mobile.yangkeduo.com/mall_page.html?mall_id=645773319","startDate":"2020-11-11","stayTime":1,"taskName":"pdd搜索流量","taskType":1,"variety":1}';
        exit;*/
        return self::curl_post($this->apiurl,$param,$header);
    }

    public function SendaApiGET()
    {

        $header = array(
            'Authorization:'.'Bearer '.$this->Authorization
        );

        //echo $this->apiurl; exit;
        return self::curl_post($this->apiurl,"",$header);
    }


    public function login()
    {
        $param['mobile'] = '15212414880';
        $param['password'] = 'fxszzwj';
        $param['domainName'] = $this->baseurl;

        if( $this->Authorization!=""){
            $res['msg']='success';
            $res['Authorization']=$this->Authorization;
            return  $res;
        }

        $param = json_encode($param);
        $header = array(
            'Content-Type:' . 'application/json;charset=UTF-8'
        );
        $res = self::curl_post($this->baseurl.'/api/app/login', $param, $header);
       // $res='{"msg":"success","Authorization":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTg1IiwiaWF0IjoxNjE0MDYyNDM3LCJleHAiOjE2MTQ2NjcyMzd9.kopO6l2lr7IjB_a0cVWzEGblYACxUNOw7M0WdkDhpZaTln7RrVkjN-WGsqhJlE9H4FX41tBFBrBH9IGOsOHOnA","code":0,"expire":604800}';
        $res=json_decode($res,true);
        if(isset($res['Authorization'])){
            $this->Authorization=$res['Authorization'];
        }
        return $res;
    }

    /**
     * 确认接口的参数和路由
     */
    public function apiparam($param, $apiType,$pass=false)
    {
        $must = ["taskType", 'skuNumber', 'startDate', 'endDate', 'dayNum', 'type', 'percent'];//必选
        $must2 = ["keyword"];
        $Optional1 = ['imgUrl','taskName'];//可选
        $Optional2 = [''];

        $numbertype=['variety','taskType','dayNum','type','stayTime','shopAround','itemOrigin','itemNum','itemTaskStyle','shopNum','shopTaskStyle'
        ,'cartNum','cartTaskStyle','commitNum','commitTaskStyle','taskStyle','num','fgType','browseOther','browseComment','pvuv',
            'addLikeItem','commentType','isCart','isShop','isItem'];

        if($apiType == '京东人气任务') {
            $this->apiurl = $this->baseurl . "/api/app/jdPop/issueApi";
            $must = array_merge($must, ["stayTime", 'shopAround', 'itemOrigin', "keywords"]);
            if ($param['tasktype'] == 2) {
                $must2 = array_merge($must2, ["commitNum", 'commitTaskStyle', "commitTaskScatter"]);
            } else {
                $must2 = array_merge($must2, ["itemNum", 'itemTaskStyle', "itemTaskScatter", "shopNum", "shopTaskStyle",
                    "shopTaskScatter", "cartNum", "cartTaskStyle", "cartTaskScatter"]);
            }
        }
        if ($apiType == '京东流量任务') {//通过类型ID拼凑参数
            $this->apiurl = $this->baseurl . "/api/app/jdFlow/issueApi";
            $must = array_merge($must, ["stayTime", 'shopAround', "keywords", 'itemOrigin']);
            unset($must[0]);//删除任务类型
            $must2 = array_merge($must2, ["keyword", 'num', "taskStyle", "taskScatter"]);
        }
        if ($apiType == '京东快车任务') {
            $this->apiurl = $this->baseurl . "/api/app/jdFast/issueApi";
            $must = array_merge($must, ["skuNumber", "keywords"]);
            $must2 = array_merge($must2, ["keyword", 'num', "taskStyle", "taskScatter"]);
        }

        if ($apiType == '京东无痕点击') {
            unset($must[0]);//删除任务类型
            $this->apiurl = $this->baseurl . "/api/app/jdValue/issueApi";
            $must = array_merge($must, ["itemOrigin", "keywords"]);
            $must2 = array_merge($must2, ["keyword", 'num', "taskStyle", "taskScatter"]);
        }
        if ($apiType == '京东发现好货') {
            $this->apiurl = $this->baseurl . "/api/app/jdFind/issueApi";
            $must = array_merge($must, ["skuNumber", "commentUrl", "num", "taskStyle", "taskScatter"]);

        }
        if ($apiType == '京东综合优化') {
            $this->apiurl = $this->baseurl . "/api/app/jdOptimize/issueApi";
            $must = array_merge($must, ["keyNumber", "commentUrl", "itemOrigin", "num", "taskStyle", "taskScatter"]);
            if($param['tasktype']==22){
                unset($must[1]);
            }
            unset($must[1]);
            unset($must[5]);
        }
        if (strstr($apiType, '拼多多') !== false) {
            $this->apiurl = $this->baseurl . "/api/app/pdd/issueApi";
            $must = array_merge($must, ["variety",'shopUrl','fgType','browseOther','itemId','itemUrl'
            ,'browseComment','pvuv','keywords','stayTime','shopAround']);
            unset($must[1]);
            unset($must[5]);
            unset($Optional1[0]);
            $must2 = array_merge($must2, ["keyword", 'num', "taskStyle", "taskScatter"]);
        }

        if (strstr($apiType, '淘宝') !== false) {
            $must = array_merge($must, ["variety",'itemUrl','fgType','browseOther'
                ,'browseComment','pvuv','keywords','stayTime','shopAround']);
            unset($must[1]);
            unset($must[5]);
            unset($Optional1[0]);
            $Optional1 = array_merge($Optional1, ["itemName"]);
            if ($apiType == '淘宝流量任务'){
                $this->apiurl = $this->baseurl . "/api/app/tbFlow/issueApi";
                $must2 = array_merge($must2, ['num', "taskStyle", "taskScatter"]);
                $Optional1=array_merge($Optional1, ['itemId','itemUrl','itemName']);
                if($param['tasktype']==4){//如果为星推荐则需要关注
                    $must = array_merge($must, ["addLikeItem",'commentType']);
                    if($param['commenttype']==2){
                        $must[]='commentUrl';
                    }
                }

                if($param['tasktype']==41){
                    $must = array_merge($must, ['itemId','itemUrl',"otherUrl"]);
                }
            }

            if ($apiType == '淘宝收藏加购任务'){
                $this->apiurl = $this->baseurl . "/api/app/tbPop/issueApi";
                $must = array_merge($must, ["itemId",'shopId','shopName']);
                $Optional1=array_merge($Optional1, ['itemName','otherUrl']);
                $must2 = array_merge($must2, ["itemNum", 'itemTaskStyle', "itemTaskScatter", "shopNum", "shopTaskStyle",
                    "shopTaskScatter", "cartNum", "cartTaskStyle", "cartTaskScatter"]);
            }

            if ($apiType == '淘宝淘宝领券'){
                $this->apiurl = $this->baseurl . "/api/app/tbFlow/issueApi";
                $Optional1=array_merge($Optional1, ['itemId','itemUrl','itemName']);
                $must2 = array_merge($must2, ['num', "taskStyle", "taskScatter"]);
            }


            if ($apiType == '淘宝高淘气值流量'||$apiType == '淘宝多路径流量任务'){
                $this->apiurl = $this->baseurl . "/api/app/tbTag/issueApi";

                $must = array_merge($must, ["itemId",'shopId','shopName','sellerId','isRatio']);
                $Optional1=array_merge($Optional1, ['itemName']);
                $must2 = array_merge($must2, ['num', "taskStyle", "taskScatter"]);
                if($param['isratio']==2){
                    $must = array_merge($must, ['manRatio','womanRatio']);
                }
                if($apiType=="淘宝高淘气值流量"){
                    $must = array_merge($must, ["category"]);
                }

            }
            if ($apiType == '淘宝店铺粉丝'){
                unset($must[8]);
                unset($must[9]);
                unset($must[10]);
                unset($must[11]);
                unset($must[12]);
                unset($must[14]);
                unset($must[15]);
                $must = array_merge($must, ["otherUrl"]);
                $this->apiurl = $this->baseurl . "/api/app/tbFlow/issueApi";
                $Optional1=array_merge($Optional1, ['taskName']);
                $Optional2=array_merge($Optional2, ['keyword']);
                unset($must2[0]);
                $must2 = array_merge($must2, ['num', "taskStyle", "taskScatter"]);
            }
            if($apiType == '淘宝人气爆款套餐任务'){
                $this->apiurl = $this->baseurl . "/api/app/tbFlow/issueApi";
                $must = array_merge($must, ["shopId"]);
                $Optional1=array_merge($Optional1, ['itemId','itemUrl','itemName']);
                $must2 = array_merge($must2, ['num', "taskStyle", "taskScatter"]);
                if($param['tasktype']==47){
                    $must = array_merge($must, ["otherUrl"]);
                }

            }



        }

        $final = array();
        if (in_array('taskStyle', $must)) {
            $param['keyword'] = $param['keywords'][0]['keyword'];
            $param['taskstyle'] = $param['keywords'][0]['taskstyle'];
            $param['num'] = $param['keywords'][0]['num'];
            $param['taskscatter'] = $param['keywords'][0]['taskscatter'];
        }

        foreach ($must as $id => $v) {
            $v2 = strtolower($v);
            if (isset($param[$v2])) {
                if ($param[$v2] == "") {
                    $msg['code'] = 1;
                    $msg['msg'] = "缺少参数" . $v;
                    if(!$pass){
                        return $msg;
                    }
                }
                if(in_array($v,$numbertype)){
                    $param[$v2]=intval($param[$v2]);
                }else{
                    if($v=='percent'){
                        $param[$v2]=round($param[$v2],2);
                    }
                }
                $final[$v] = $param[$v2];
            } else {
                $msg['code'] = 1;
                $msg['msg'] = "缺少参数" . $v;
                if(!$pass){
                    return $msg;
                }
            }
        }

        foreach ($Optional1 as $id => $v) {
            $v2 = strtolower($v);
            if (isset($param[$v2])) {
                $final[$v] = $param[$v2];
            } else {
                $final[$v] = "";
            }
        }
        if (isset($final['keywords'])) {
            $final['keywords'] = array();
            foreach ($must2 as $id => $v) {
                $lowerv = strtolower($v);
                foreach ($param['keywords'] as $key => $value) {
                    if (isset($value[$lowerv])) {
                        if (strpos($v, 'Style')) {
                            $value[$lowerv]=intval($value[$lowerv]);
                        }
                        if(in_array($v,$numbertype)){
                            $value[$lowerv]=intval( $value[$lowerv]);
                        }
                        $final['keywords'][$key][$v] = $value[$lowerv];
                    } else {
                        $msg['code'] = 1;
                        $msg['msg'] = "缺少参数" . $lowerv;
                        if(!$pass){
                            return $msg;
                        }
                    }
                }
            }

            foreach ($Optional2 as $id=>$v){
                $lowerv = strtolower($v);
                foreach ($param['keywords'] as $key => $value) {
                    if (isset($value[$lowerv])) {
                        if (strpos($v, 'Style')) {
                            $value[$lowerv]=intval($value[$lowerv]);
                        }
                        if(in_array($v,$numbertype)){
                            $value[$lowerv]=intval( $value[$lowerv]);
                        }
                        $final['keywords'][$key][$v] = $value[$lowerv];
                    }
                }
            }
        }
        return $final;
    }


    public function getapiurl($platformType="",$type="",$file='getDetail'){

        $row[1]=[
            1=>'jdPop',
            2=>'jdFlow',
            3=>'jdFast',
            4=>'jdValue',
            5=>'jdFind',
            6=>'jdOptimize',
        ];
        $row[2]='pdd';
        $row[3]=[
            1=>'tbFlow',
            2=>'tbPop',
            3=>'tbFlow',
            6=>'tbTag',
            7=>'tbTag',
            9=>'tbFlow',
            10=>'tbFlow',
        ];
        $typelist=$row[$platformType];
        if(is_array($typelist)){
            $vlaue=$typelist[$type];
        }else{
            $vlaue=$typelist;
        }
        $this->apiurl = $this->baseurl . "/api/app/".$vlaue."/".$file;
        return $this->apiurl;
    }


    //  post请求
    private function curl_post($url, $curlPost = "", $header = "")
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        if ($curlPost) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        }
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    //  获取13位时间戳
    private static function getMillisecond()
    {
        list($t1, $t2) = explode(' ', microtime());
        return sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
    }
}
